<?php
/**
 * @author Tibor Pino (tibor.pino@gmail.com)
 * @license released under GNU Public License v. 2
 * @copyright Copyright (c) 2007, Tibor Pino
 *
 */

class CommentsController extends AppController {

    public $helpers = array('Rss','Text','Pagination');

    public function admin_index() {
        $this->Comment->recursive = 1;
        $this->paginate = array('order' => 'Comment.created desc');
        $this->set('comments', $this->paginate());
    }

    public function admin_edit($id = null) {
        if (!$this->Comment->findById($id)) {
            $this->Session->setFlash(__('Invalid comment',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->Comment->save($this->data)) {
                $this->Session->setFlash(__('The Comment has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash('Comment could not be saved. Please, try again.');
            }
        } else {
            $this->data = $this->Comment->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
        if (!$this->Comment->findById($id)) {
            $this->Session->setFlash(__('Invalid comment',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if ($this->Comment->del($id)) {
            $this->Session->setFlash(__('Comment deleted',true));
        } else {
            $this->Session->setFlash(__('The Comment could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_comments() {
        if($this->Comment->deleteAll(array('Comment.id'=>$this->data['CommentsDelete']))) {
            $this->Session->setFlash(__('Comments deleted',true));
        } else {
            $this->Session->setFlash(__('The Comments could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_spam() {
        if($this->Comment->deleteAll(array("antispam<>''"))) {
            $this->Session->setFlash(__('Comments deleted',true));
        } else {
            $this->Session->setFlash(__('The Comments could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function index() {
        $this->paginate = array('order'=>'Comment.created desc','conditions'=>'Comment.antispam ="" or Comment.antispam is null');
        $this->set('comments',$this->paginate());
    }

    public function rss() {
        $this->set('comments',$this->Comment->find_all());
    }

    public function sidebox() {
        return $this->Comment->last();
    }
}
?>